﻿// ---------------------------------------------------------------------------------------------------------------------
// 名    称：网络硬盘数据操作类
// 功    能：提供网络硬盘数据的各种操作方法
// 创建日期：2012.10.9
// 创 建 人：刘剑

// 修改时间               修改人                  修改描述                                          备注
// ---------------------------------------------------------------------------------------------------------------------
// 
// ---------------------------------------------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.Common;

using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using AnyOffice.Model;//请先添加引用

namespace AnyOffice.DAL
{
    /// <summary>
    /// 网络硬盘数据操作类
    /// </summary>
    public class DiskGXDao
    {
        public DiskGXDao()
        { }
        #region  BasicMethod





        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SqlConnection conn, DiskGX model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DiskGX(");
            strSql.Append("Seq,ParentDiskID,UserID,FileTypeID,Name,Keyword,ShareStartTime,ShareEndTime,ShareDept,ShareRole,ShareUser,ReadUser,WriteUser,DownloadUser,ReadDept,WriteDept,DownloadDept,ReadRole,WriteRole,DownloadRole,UpdateTime,DiskTypeID,FileSize)");
            strSql.Append(" values (");
            strSql.Append("@Seq,@ParentDiskID,@UserID,@FileTypeID,@Name,@Keyword,@ShareStartTime,@ShareEndTime,@ShareDept,@ShareRole,@ShareUser,@ReadUser,@WriteUser,@DownloadUser,@ReadDept,@WriteDept,@DownloadDept,@ReadRole,@WriteRole,@DownloadRole,@UpdateTime,@DiskTypeID,@FileSize) SELECT SCOPE_IDENTITY() AS RetValue");
            SqlParameter[] parameters = {
					
					new SqlParameter("@Seq", SqlDbType.Int,4),
					new SqlParameter("@ParentDiskID", SqlDbType.BigInt,8),
					new SqlParameter("@UserID", SqlDbType.VarChar,100),
					new SqlParameter("@FileTypeID", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.VarChar,500),
					new SqlParameter("@Keyword", SqlDbType.VarChar,500),
					new SqlParameter("@ShareStartTime", SqlDbType.DateTime),
					new SqlParameter("@ShareEndTime", SqlDbType.DateTime),
					new SqlParameter("@ShareDept", SqlDbType.VarChar,500),
					new SqlParameter("@ShareRole", SqlDbType.VarChar,500),
					new SqlParameter("@ShareUser", SqlDbType.VarChar,500),
					new SqlParameter("@ReadUser", SqlDbType.VarChar,500),
					new SqlParameter("@WriteUser", SqlDbType.VarChar,500),
					new SqlParameter("@DownloadUser", SqlDbType.VarChar,500),
					new SqlParameter("@ReadDept", SqlDbType.VarChar,500),
					new SqlParameter("@WriteDept", SqlDbType.VarChar,500),
					new SqlParameter("@DownloadDept", SqlDbType.VarChar,500),
					new SqlParameter("@ReadRole", SqlDbType.VarChar,500),
					new SqlParameter("@WriteRole", SqlDbType.VarChar,500),
					new SqlParameter("@DownloadRole", SqlDbType.VarChar,500),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@DiskTypeID", SqlDbType.Int,4),
					new SqlParameter("@FileSize", SqlDbType.Decimal,13)};
         
            parameters[0].Value = model.Seq;
            parameters[1].Value = model.ParentDiskID;
            parameters[2].Value = model.UserID;
            parameters[3].Value = model.FileTypeID;
            parameters[4].Value = model.Name;
            parameters[5].Value = model.Keyword;
            parameters[6].Value = model.ShareStartTime;
            parameters[7].Value = model.ShareEndTime;
            parameters[8].Value = model.ShareDept;
            parameters[9].Value = model.ShareRole;
            parameters[10].Value = model.ShareUser;
            parameters[11].Value = model.ReadUser;
            parameters[12].Value = model.WriteUser;
            parameters[13].Value = model.DownloadUser;
            parameters[14].Value = model.ReadDept;
            parameters[15].Value = model.WriteDept;
            parameters[16].Value = model.DownloadDept;
            parameters[17].Value = model.ReadRole;
            parameters[18].Value = model.WriteRole;
            parameters[19].Value = model.DownloadRole;
            parameters[20].Value = model.UpdateTime;
            parameters[21].Value = model.DiskTypeID;
            parameters[22].Value = model.FileSize;


            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, strSql.ToString(), parameters);
            if (obj2 != DBNull.Value)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;


        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SqlConnection conn, DiskGX model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DiskGX set ");
            strSql.Append("Seq=@Seq,");
            strSql.Append("ParentDiskID=@ParentDiskID,");
            strSql.Append("UserID=@UserID,");
            strSql.Append("FileTypeID=@FileTypeID,");
            strSql.Append("Name=@Name,");
            strSql.Append("Keyword=@Keyword,");
            strSql.Append("ShareStartTime=@ShareStartTime,");
            strSql.Append("ShareEndTime=@ShareEndTime,");
            strSql.Append("ShareDept=@ShareDept,");
            strSql.Append("ShareRole=@ShareRole,");
            strSql.Append("ShareUser=@ShareUser,");
            strSql.Append("ReadUser=@ReadUser,");
            strSql.Append("WriteUser=@WriteUser,");
            strSql.Append("DownloadUser=@DownloadUser,");
            strSql.Append("ReadDept=@ReadDept,");
            strSql.Append("WriteDept=@WriteDept,");
            strSql.Append("DownloadDept=@DownloadDept,");
            strSql.Append("ReadRole=@ReadRole,");
            strSql.Append("WriteRole=@WriteRole,");
            strSql.Append("DownloadRole=@DownloadRole,");
            strSql.Append("UpdateTime=@UpdateTime,");
            strSql.Append("DiskTypeID=@DiskTypeID,");
            strSql.Append("FileSize=@FileSize");
            strSql.Append(" where DiskID=@DiskID ");
            SqlParameter[] parameters = {
					new SqlParameter("@Seq", SqlDbType.Int,4),
					new SqlParameter("@ParentDiskID", SqlDbType.BigInt,8),
					new SqlParameter("@UserID", SqlDbType.VarChar,100),
					new SqlParameter("@FileTypeID", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.VarChar,500),
					new SqlParameter("@Keyword", SqlDbType.VarChar,500),
					new SqlParameter("@ShareStartTime", SqlDbType.DateTime),
					new SqlParameter("@ShareEndTime", SqlDbType.DateTime),
					new SqlParameter("@ShareDept", SqlDbType.VarChar,500),
					new SqlParameter("@ShareRole", SqlDbType.VarChar,500),
					new SqlParameter("@ShareUser", SqlDbType.VarChar,500),
					new SqlParameter("@ReadUser", SqlDbType.VarChar,500),
					new SqlParameter("@WriteUser", SqlDbType.VarChar,500),
					new SqlParameter("@DownloadUser", SqlDbType.VarChar,500),
					new SqlParameter("@ReadDept", SqlDbType.VarChar,500),
					new SqlParameter("@WriteDept", SqlDbType.VarChar,500),
					new SqlParameter("@DownloadDept", SqlDbType.VarChar,500),
					new SqlParameter("@ReadRole", SqlDbType.VarChar,500),
					new SqlParameter("@WriteRole", SqlDbType.VarChar,500),
					new SqlParameter("@DownloadRole", SqlDbType.VarChar,500),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@DiskTypeID", SqlDbType.Int,4),
					new SqlParameter("@FileSize", SqlDbType.Decimal,13),
					new SqlParameter("@DiskID", SqlDbType.BigInt,8)};
            parameters[0].Value = model.Seq;
            parameters[1].Value = model.ParentDiskID;
            parameters[2].Value = model.UserID;
            parameters[3].Value = model.FileTypeID;
            parameters[4].Value = model.Name;
            parameters[5].Value = model.Keyword;
            parameters[6].Value = model.ShareStartTime;
            parameters[7].Value = model.ShareEndTime;
            parameters[8].Value = model.ShareDept;
            parameters[9].Value = model.ShareRole;
            parameters[10].Value = model.ShareUser;
            parameters[11].Value = model.ReadUser;
            parameters[12].Value = model.WriteUser;
            parameters[13].Value = model.DownloadUser;
            parameters[14].Value = model.ReadDept;
            parameters[15].Value = model.WriteDept;
            parameters[16].Value = model.DownloadDept;
            parameters[17].Value = model.ReadRole;
            parameters[18].Value = model.WriteRole;
            parameters[19].Value = model.DownloadRole;
            parameters[20].Value = model.UpdateTime;
            parameters[21].Value = model.DiskTypeID;
            parameters[22].Value = model.FileSize;
            parameters[23].Value = model.DiskID;

            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql.ToString(), parameters);
            return true;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(SqlConnection conn, long DiskID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DiskGX ");
            strSql.Append(" where DiskID=@DiskID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DiskID", SqlDbType.BigInt,8)			};
            parameters[0].Value = DiskID;

            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql.ToString(), parameters);
            return true;
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DiskGX GetModel(SqlConnection conn, long DiskID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 DiskID,Seq,ParentDiskID,UserID,FileTypeID,Name,Keyword,ShareStartTime,ShareEndTime,ShareDept,ShareRole,ShareUser,ReadUser,WriteUser,DownloadUser,ReadDept,WriteDept,DownloadDept,ReadRole,WriteRole,DownloadRole,UpdateTime,DiskTypeID,FileSize from DiskGX ");
            strSql.Append(" where DiskID=@DiskID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DiskID", SqlDbType.BigInt,8)			};
            parameters[0].Value = DiskID;

            DiskGX model = new DiskGX();

            DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DiskGX DataRowToModel(DataRow row)
        {
            DiskGX model = new DiskGX();
            if (row != null)
            {
                if (row["DiskID"] != null && row["DiskID"].ToString() != "")
                {
                    model.DiskID = long.Parse(row["DiskID"].ToString());
                }
                if (row["Seq"] != null && row["Seq"].ToString() != "")
                {
                    model.Seq = int.Parse(row["Seq"].ToString());
                }
                if (row["ParentDiskID"] != null && row["ParentDiskID"].ToString() != "")
                {
                    model.ParentDiskID = long.Parse(row["ParentDiskID"].ToString());
                }
                if (row["UserID"] != null)
                {
                    model.UserID = row["UserID"].ToString();
                }
                if (row["FileTypeID"] != null && row["FileTypeID"].ToString() != "")
                {
                    model.FileTypeID = int.Parse(row["FileTypeID"].ToString());
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["Keyword"] != null)
                {
                    model.Keyword = row["Keyword"].ToString();
                }
                if (row["ShareStartTime"] != null && row["ShareStartTime"].ToString() != "")
                {
                    model.ShareStartTime = DateTime.Parse(row["ShareStartTime"].ToString());
                }
                if (row["ShareEndTime"] != null && row["ShareEndTime"].ToString() != "")
                {
                    model.ShareEndTime = DateTime.Parse(row["ShareEndTime"].ToString());
                }
                if (row["ShareDept"] != null)
                {
                    model.ShareDept = row["ShareDept"].ToString();
                }
                if (row["ShareRole"] != null)
                {
                    model.ShareRole = row["ShareRole"].ToString();
                }
                if (row["ShareUser"] != null)
                {
                    model.ShareUser = row["ShareUser"].ToString();
                }
                if (row["ReadUser"] != null)
                {
                    model.ReadUser = row["ReadUser"].ToString();
                }
                if (row["WriteUser"] != null)
                {
                    model.WriteUser = row["WriteUser"].ToString();
                }
                if (row["DownloadUser"] != null)
                {
                    model.DownloadUser = row["DownloadUser"].ToString();
                }
                if (row["ReadDept"] != null)
                {
                    model.ReadDept = row["ReadDept"].ToString();
                }
                if (row["WriteDept"] != null)
                {
                    model.WriteDept = row["WriteDept"].ToString();
                }
                if (row["DownloadDept"] != null)
                {
                    model.DownloadDept = row["DownloadDept"].ToString();
                }
                if (row["ReadRole"] != null)
                {
                    model.ReadRole = row["ReadRole"].ToString();
                }
                if (row["WriteRole"] != null)
                {
                    model.WriteRole = row["WriteRole"].ToString();
                }
                if (row["DownloadRole"] != null)
                {
                    model.DownloadRole = row["DownloadRole"].ToString();
                }
                if (row["UpdateTime"] != null && row["UpdateTime"].ToString() != "")
                {
                    model.UpdateTime = DateTime.Parse(row["UpdateTime"].ToString());
                }
                if (row["DiskTypeID"] != null && row["DiskTypeID"].ToString() != "")
                {
                    model.DiskTypeID = int.Parse(row["DiskTypeID"].ToString());
                }
                if (row["FileSize"] != null && row["FileSize"].ToString() != "")
                {
                    model.FileSize = decimal.Parse(row["FileSize"].ToString());
                }
            }
            return model;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public AnyOffice.Model.DiskGX ReaderBind2(IDataReader dataReader)
        {
            AnyOffice.Model.DiskGX model = new AnyOffice.Model.DiskGX();
            object ojb;
            ojb = dataReader["DiskID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DiskID = (long)ojb;
            }
            ojb = dataReader["ParentDiskID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ParentDiskID = (long)ojb;
            }
            model.UserID = dataReader["UserID"].ToString();

            model.Name = dataReader["Name"].ToString();
            ojb = dataReader["DiskTypeID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DiskTypeID = (int)ojb;
            }
            return model;
        }

        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public AnyOffice.Model.DiskGX ReaderBind(IDataReader dataReader)
        {
            AnyOffice.Model.DiskGX model = new AnyOffice.Model.DiskGX();
            object ojb;
            ojb = dataReader["DiskID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DiskID = (long)ojb;
            }
            ojb = dataReader["ParentDiskID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ParentDiskID = (long)ojb;
            }
            model.UserID = dataReader["UserID"].ToString();

            ojb = dataReader["FileTypeID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.FileTypeID = (int)ojb;
            }
            model.Name = dataReader["Name"].ToString();
            model.Keyword = dataReader["Keyword"].ToString();
            ojb = dataReader["ShareStartTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ShareStartTime = (DateTime)ojb;
            }
            ojb = dataReader["ShareEndTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ShareEndTime = (DateTime)ojb;
            }
            model.ShareDept = dataReader["ShareDept"].ToString();
            model.ShareRole = dataReader["ShareRole"].ToString();
            model.ShareUser = dataReader["ShareUser"].ToString();
            model.ReadUser = dataReader["ReadUser"].ToString();
            model.WriteUser = dataReader["WriteUser"].ToString();
            model.DownloadUser = dataReader["DownloadUser"].ToString();
            model.ReadDept = dataReader["ReadDept"].ToString();
            model.WriteDept = dataReader["WriteDept"].ToString();
            model.DownloadDept = dataReader["DownloadDept"].ToString();
            model.ReadRole = dataReader["ReadRole"].ToString();
            model.WriteRole = dataReader["WriteRole"].ToString();
            model.DownloadRole = dataReader["DownloadRole"].ToString();
            ojb = dataReader["UpdateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UpdateTime = (DateTime)ojb;
            }
            ojb = dataReader["DiskTypeID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DiskTypeID = (int)ojb;
            }
            ojb = dataReader["FileSize"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.FileSize = (decimal)ojb;
            }
            ojb = dataReader["Seq"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Seq = (int)ojb;
            }
            return model;
        }



        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetByNewsId(SqlConnection conn, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DiskID,ParentDiskID,UserID,FileTypeID,Name,Keyword,ShareStartTime,ShareEndTime,ReadUser,WriteUser,DownloadUser,ReadDept,WriteDept,DownloadDept,ReadRole,WriteRole,DownloadRole,UpdateTime,DiskTypeID,FileSize,Seq,");
            strSql.Append("dbo.GetUserNames(CAST(WriteUser AS nvarchar(2000))) AS ToManger, dbo.GetDeptNames(CAST(ReadDept AS nvarchar(2000))) AS ToReadDept,dbo.GetPrivNames(CAST(ReadRole AS nvarchar(2000))) AS ToReadRole,");
            strSql.Append("dbo.GetUserNames(CAST(ReadUser AS nvarchar(2000))) AS ToReadUser, dbo.GetDeptNames(CAST(DownloadDept AS nvarchar(2000))) AS ToDownDept,dbo.GetPrivNames(CAST(DownloadRole AS nvarchar(2000))) AS ToDownRole,");
            strSql.Append("dbo.GetUserNames(CAST(DownloadUser AS nvarchar(2000))) AS ToDowUser");
            strSql.Append(" FROM DiskGX ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }


            return SqlHelper.ExecuteDataset(conn, CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetShareById(SqlConnection conn, string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DiskID,ParentDiskID,UserID,FileTypeID,Name,ShareStartTime,ShareEndTime,ShareDept,ShareRole,ShareUser,UpdateTime,DiskTypeID,FileSize,Seq,");
            strSql.Append("dbo.GetDeptNames(CAST(ShareDept AS nvarchar(2000))) AS ToShareDept,dbo.GetPrivNames(CAST(ShareRole AS nvarchar(2000))) AS ToShareRole,");
            strSql.Append(" dbo.GetUserNames(CAST(ShareUser AS nvarchar(2000))) AS ToShareUser  FROM DiskGX  where DiskID=" + id + "");

            return SqlHelper.ExecuteDataset(conn, CommandType.Text, strSql.ToString());
        }



        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(SqlConnection conn, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DiskID,Seq,ParentDiskID,UserID,FileTypeID,Name,Keyword,ShareStartTime,ShareEndTime,ShareDept,ShareRole,ShareUser,ReadUser,WriteUser,DownloadUser,ReadDept,WriteDept,DownloadDept,ReadRole,WriteRole,DownloadRole,UpdateTime,DiskTypeID,FileSize ");
            strSql.Append(" FROM DiskGX ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.ExecuteDataset(conn, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(SqlConnection conn, int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" DiskID,Seq,ParentDiskID,UserID,FileTypeID,Name,Keyword,ShareStartTime,ShareEndTime,ShareDept,ShareRole,ShareUser,ReadUser,WriteUser,DownloadUser,ReadDept,WriteDept,DownloadDept,ReadRole,WriteRole,DownloadRole,UpdateTime,DiskTypeID,FileSize ");
            strSql.Append(" FROM DiskGX ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SqlHelper.ExecuteDataset(conn, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(SqlConnection conn, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM DiskGX ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            object obj = SqlHelper.ExecuteScalar(conn, CommandType.Text, strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        #endregion  BasicMethod

        #region  成员方法  cth

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<AnyOffice.Model.DiskGX> GetModelList(SqlConnection conn, string strWhere)
        {
            DataSet ds = GetList(conn, strWhere);
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<AnyOffice.Model.DiskGX> DataTableToList(DataTable dt)
        {
            List<AnyOffice.Model.DiskGX> modelList = new List<AnyOffice.Model.DiskGX>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                AnyOffice.Model.DiskGX model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new AnyOffice.Model.DiskGX();
                    if (dt.Rows[n]["DiskID"].ToString() != "")
                    {
                        model.DiskID = long.Parse(dt.Rows[n]["DiskID"].ToString());
                    }
                    if (dt.Rows[n]["ParentDiskID"].ToString() != "")
                    {
                        model.ParentDiskID = long.Parse(dt.Rows[n]["ParentDiskID"].ToString());
                    }

                    if (dt.Rows[n]["UserID"].ToString() != "")
                    {
                        model.UserID = dt.Rows[n]["UserID"].ToString();
                    }
                    if (dt.Rows[n]["FileTypeID"].ToString() != "")
                    {
                        model.FileTypeID = int.Parse(dt.Rows[n]["FileTypeID"].ToString());
                    }
                    model.Name = dt.Rows[n]["Name"].ToString();
                    model.Keyword = dt.Rows[n]["Keyword"].ToString();
                    if (dt.Rows[n]["ShareStartTime"].ToString() != "")
                    {
                        model.ShareStartTime = DateTime.Parse(dt.Rows[n]["ShareStartTime"].ToString());
                    }
                    if (dt.Rows[n]["ShareEndTime"].ToString() != "")
                    {
                        model.ShareEndTime = DateTime.Parse(dt.Rows[n]["ShareEndTime"].ToString());
                    }
                    model.ShareDept = dt.Rows[n]["ShareDept"].ToString();
                    model.ShareRole = dt.Rows[n]["ShareRole"].ToString();
                    model.ShareUser = dt.Rows[n]["ShareUser"].ToString();
                    model.ReadUser = dt.Rows[n]["ReadUser"].ToString();
                    model.WriteUser = dt.Rows[n]["WriteUser"].ToString();
                    model.DownloadUser = dt.Rows[n]["DownloadUser"].ToString();
                    model.ReadDept = dt.Rows[n]["ReadDept"].ToString();
                    model.WriteDept = dt.Rows[n]["WriteDept"].ToString();
                    model.DownloadDept = dt.Rows[n]["DownloadDept"].ToString();
                    model.ReadRole = dt.Rows[n]["ReadRole"].ToString();
                    model.WriteRole = dt.Rows[n]["WriteRole"].ToString();
                    model.DownloadRole = dt.Rows[n]["DownloadRole"].ToString();
                    if (dt.Rows[n]["UpdateTime"].ToString() != "")
                    {
                        model.UpdateTime = DateTime.Parse(dt.Rows[n]["UpdateTime"].ToString());
                    }
                    if (dt.Rows[n]["DiskTypeID"].ToString() != "")
                    {
                        model.DiskTypeID = int.Parse(dt.Rows[n]["DiskTypeID"].ToString());
                    }
                    if (dt.Rows[n]["FileSize"].ToString() != "")
                    {
                        model.FileSize = decimal.Parse(dt.Rows[n]["FileSize"].ToString());
                    }

                    if (dt.Rows[n]["Seq"].ToString() != "")
                    {
                        model.FileSize = int.Parse(dt.Rows[n]["Seq"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 根据父节点的到子节点列表
        /// </summary>
        public List<AnyOffice.Model.DiskGX> GetListByparentId(SqlConnection conn, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DiskID,Name,ParentDiskID,UserID,DiskTypeID,Seq");
            strSql.Append(" FROM DiskGX ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<AnyOffice.Model.DiskGX> list = new List<AnyOffice.Model.DiskGX>();


            using (IDataReader dataReader = SqlHelper.ExecuteReader(conn, CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind2(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 获得数据列表 用户ID转换用户名
        /// </summary>
        public DataSet GetDiskMangerList(SqlConnection conn, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DiskID,ParentDiskID,UserID,FileTypeID,Name,Seq,dbo.GetUserNames(CAST(WriteUser AS nvarchar(2000))) AS WriteUser FROM DiskGX");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            return SqlHelper.ExecuteDataset(conn, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 移动目录
        /// </summary>
        /// <returns></returns>
        public int UpdateParentId(SqlConnection conn, string parentid, string strid)
        {
            try
            {
                string sql = "update [DiskGX] set ParentDiskID=" + parentid + " where DiskID in (" + strid + ")";

                return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql);

            }
            catch
            {
                return -1;
            }
        }

        /// <summary>
        ///  批量删除
        /// </summary>
        /// <returns></returns>
        public int DelbyId(SqlConnection conn, string sstrql, string strid)
        {
            try
            {
                int x = 0;
                string strSql2 = "delete from DiskGX where DiskID in (" + strid + ")";




                x = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql2);

                DataSet ds = GetList(conn, " ParentDiskID in(" + strid + ")");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        string id = ds.Tables[0].Rows[i]["DiskID"].ToString();
                        string strSql = "delete from DiskGX where DiskID=" + id;
                        SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql);

                        DelbyId(conn, sstrql, id);
                    }
                }
                return x;
            }
            catch
            {
                return -1;
            }
        }


        /// <summary>
        /// 删除一条是文件夹得数据
        /// </summary>
        public int DeleteFolad(SqlConnection conn, long DiskID)
        {
            int x = 0;
            string strSql2 = "delete from DiskGX where DiskID=" + DiskID;
            x = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql2);

            DataSet ds = GetList(conn, " ParentDiskID=" + DiskID);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string id = ds.Tables[0].Rows[i]["DiskID"].ToString();
                    string strSql = "delete from DiskGX where DiskID=" + id;
                    SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql);
                    DeleteFolad(conn, long.Parse(id));
                }
            }
            return x;
        }


        /// <summary>
        /// 获得目录路径
        /// </summary>
        /// <param name="diskid"></param>
        /// <returns></returns>
        public string GetParentPath(SqlConnection conn, string diskid, string basepath)
        {

            string strSql = "select DiskID,[Name],ParentDiskID from [DiskGX] where [DiskID]=" + diskid + "";

            DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, strSql.ToString());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                if (basepath != "")
                {
                    basepath = ds.Tables[0].Rows[0]["Name"].ToString() + "\\" + basepath;
                }
                else
                {
                    basepath = ds.Tables[0].Rows[0]["Name"].ToString();
                }

                basepath = GetParentPath(conn, ds.Tables[0].Rows[0]["ParentDiskID"].ToString(), basepath);

            }

            return basepath;


        }


        /// <summary>
        /// 获取文件名称
        /// </summary>
        /// <param name="diskid">文件（夹）id</param>
        /// <returns>文件名</returns>
        public string GetFileName(SqlConnection conn, string diskid)
        {
            string fileName = String.Empty;

            string strSql = "select Name from DiskGX where DiskID=" + diskid + "";

            DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, strSql.ToString());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                fileName = ds.Tables[0].Rows[0]["Name"].ToString();
            }
            return fileName;
        }

        /// <summary>
        /// 判断同一节点下名称是否存正
        /// </summary>
        /// <param name="name">文件名</param>
        /// <param name="pid">父节点</param>
        public bool IsExitName(SqlConnection conn, string name, string pid)
        {
            string sql = "select * from [DiskGX] where [Name]='" + name + "' and ParentDiskID='" + pid + "'";

            int cmdresult;
            object obj = SqlHelper.ExecuteScalar(conn, CommandType.Text, sql);


            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 个人使用大小
        /// </summary>
        /// <param name="str">查询条件</param>
        public decimal count(SqlConnection conn, string str)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sum(FileSize) from DiskGX ");
            if (str != "")
            {
                strSql.Append(" where " + str + "");
            }
            object obj = SqlHelper.ExecuteScalar(conn, CommandType.Text, strSql.ToString());
            decimal cmdresult = 0.00m;

            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0.00m;
            }
            else { cmdresult = decimal.Parse(obj.ToString()); }
            return cmdresult;
        }

        /// <summary>
        /// 或得我共享的文件
        /// </summary>
        /// <param name="UserID">用户id</param>
        public DataSet GetMyShare(SqlConnection conn, string UserID)
        {


            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.VarChar)			};
            parameters[0].Value = UserID;

            return SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, "GetMyshare", parameters);

        }


        /// <summary>
        /// 或得共享给我的文件
        /// </summary>
        /// <param name="UserID">用户id</param>
        public DataSet GetMyShare(SqlConnection conn, string UserID, string DeptID, string roleId)
        {
            //Database db = DatabaseFactory.CreateDatabase();
            //DbCommand dbCommand = db.GetStoredProcCommand("GetShare_my");
            //db.AddInParameter(dbCommand, "UserID", DbType.String, UserID);
            //db.AddInParameter(dbCommand, "DeptID", DbType.String, DeptID);
            //db.AddInParameter(dbCommand, "roleId", DbType.String, roleId);
            //return db.ExecuteDataSet(dbCommand);


            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.VarChar),
					new SqlParameter("@DeptID", SqlDbType.VarChar),
					new SqlParameter("@roleId", SqlDbType.VarChar)
                                        };
            parameters[0].Value = UserID;
            parameters[1].Value = DeptID;
            parameters[2].Value = roleId;

            return SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, "GetShare_my", parameters);

        }

        /// <summary>
        /// 更新共享 
        /// </summary>
        /// <param name="model">文档柜对象</param>
        public int UpdateShare(SqlConnection conn, AnyOffice.Model.DiskGX model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DiskGX set ");
            strSql.Append("ShareStartTime=@ShareStartTime,");
            strSql.Append("ShareEndTime=@ShareEndTime,");
            strSql.Append("ShareDept=@ShareDept,");
            strSql.Append("ShareRole=@ShareRole,");
            strSql.Append("ShareUser=@ShareUser");
            strSql.Append(" where DiskID=@DiskID ");


            SqlParameter[] parameters = {
					new SqlParameter("@ShareStartTime", SqlDbType.DateTime),
					new SqlParameter("@ShareEndTime", SqlDbType.DateTime),
					new SqlParameter("@ShareDept", SqlDbType.VarChar),
					new SqlParameter("@ShareRole", SqlDbType.VarChar),
					new SqlParameter("@ShareUser", SqlDbType.VarChar),
					new SqlParameter("@DiskID", SqlDbType.Int),
                                        };
            parameters[0].Value = model.ShareStartTime;
            parameters[1].Value = model.ShareEndTime;
            parameters[2].Value = model.ShareDept;
            parameters[3].Value = model.ShareRole;
            parameters[4].Value = model.ShareUser;
            parameters[5].Value = model.DiskID;

            return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql.ToString());



        }

        /// <summary>
        /// 或得个人文档柜的对象
        /// </summary>
        /// <param name="UserID">登录用户id</param>
        /// <returns></returns>
        public Model.DiskGX GetDiskByUserId(SqlConnection conn, string UserID)
        {
            string strSql = "select * from DiskGX where UserID='" + UserID + "' and [Name]='" + UserID + "' and ParentDiskID=0";


            AnyOffice.Model.DiskGX model = null;
            using (IDataReader dataReader = SqlHelper.ExecuteReader(conn, CommandType.Text, strSql))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 更改个人文件柜大小
        /// </summary>
        /// <param name="size">大小</param>
        /// <param name="UserID">用户id</param>
        public int UpFolderSize(SqlConnection conn, string size, string UserID)
        {
            string sql = "update DiskGX set FileSize='" + size + "' where UserID='" + UserID + "' and [Name]='" + UserID + "' and ParentDiskID=0";
            //Database db = DatabaseFactory.CreateDatabase();
            //DbCommand dbCommand = db.GetSqlStringCommand(sql.ToString());
            //return db.ExecuteNonQuery(dbCommand);

            return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql);

        }

        /// <summary>
        /// 或得文件夹总大小
        /// </summary>
        /// <param name="id">文件夹id</param>
        public decimal GetfolderSize(SqlConnection conn, string id)
        {


            string strSql = " select sum(FileSize) as FileSize from [GetFileSize](" + id + ")";

            //DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            decimal cmdresult = 0.00m;
            object obj = SqlHelper.ExecuteScalar(conn, CommandType.Text, strSql);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0.00m;
            }
            else { cmdresult = decimal.Parse(obj.ToString()); }
            return cmdresult;
        }

        /// <summary>
        /// 根据可管理权限的父节点，返回搜索id范围
        /// </summary>
        /// <param name="ids">可管理id</param>
        public string CanSeeId(SqlConnection conn, string ids)
        {
            string strids = string.Empty;
            string[] arr = ids.Split(',');
            for (int i = 0; i < arr.Length; i++)
            {
                string sql = "select * from dbo.aa(" + arr[i] + ")";
                //Database db = DatabaseFactory.CreateDatabase();
                //DataSet ds = db.ExecuteDataSet(CommandType.Text, sql);
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        strids += ds.Tables[0].Rows[j]["DiskID"] + ",";
                    }
                }
            }
            return strids;
        }

        #endregion  成员方法

        /// <summary>
        /// 根据父ID获取文件数据
        /// </summary>
        /// <param name="id">父id</param>
        public DataTable GetChildFileInfo(SqlConnection conn, string pId)
        {

            string strSql = " select DiskID,Name from DiskGX where FileTypeID = 1 and ParentDiskID=@ParentDiskID";




            SqlParameter[] parameters = {
					new SqlParameter("@ParentDiskID", SqlDbType.Int)			};
            parameters[0].Value = pId;

            return SqlHelper.ExecuteDataset(conn, CommandType.Text, strSql.ToString(), parameters).Tables[0];

        }

        /// <summary>
        /// 根据父ID获取文件数据
        /// </summary>
        /// <param name="id">父节点id</param>
        public DataTable GetChildFile(SqlConnection conn, string pId)
        {

            string strSql = " select DiskID,Name,FileTypeID from DiskGX where  ParentDiskID=@ParentDiskID  order by  FileTypeID  desc ";
            SqlParameter[] parameters = {
					new SqlParameter("@ParentDiskID", SqlDbType.Int)			};
            parameters[0].Value = pId;

            return SqlHelper.ExecuteDataset(conn, CommandType.Text, strSql.ToString(), parameters).Tables[0];

        }



        /// <summary>
        /// 获得一级节点id  cth 2011-6-1
        /// </summary>
        /// <returns></returns>
        public int GetLeveID(SqlConnection conn, string id)
        {
            try
            {
                string sql = "select DiskID from dbo.GetParentId(" + id + ") where ParentDiskID=0";
                //Database db = DatabaseFactory.CreateDatabase();
                //DbCommand dbCommand = db.GetSqlStringCommand(sql);
                //object obj = db.ExecuteScalar(dbCommand);
                //return int.Parse(obj.ToString());
                object obj = SqlHelper.ExecuteScalar(conn, CommandType.Text, sql);
                return int.Parse(obj.ToString());
            }
            catch
            {
                return -1;
            }
        }

    }
}

